//
//  AdapterTgameUtils.swift
//  Runner
//
//  Created by wukai on 2022/1/4.
//

import Foundation
/**
 * 针对提提电竞特殊适配
 * <p>
 * 将msgId（消息唯一标示）字段插入业务请求参数map中的content字段中
 *
 * @param msgId  isTgame 是否是提提电竞IM
 * @param msgId  消息Id
 * @param msgMap 业务传递的请求参数
 * @return 是否成功
 */
class AdapterTgameUtils{
    
    public static func putMsgIdToRequestParamsMap(msgMap:[String:Any],msgId:String,isTgame:Bool)->Bool{
        var map = msgMap;
        if(isTgame){
            
            let resultJson = msgMap["content"];
            var dic =  NSObjetJohn.dictionary(withJsonString: resultJson as! String);
            dic["msgId"] = msgId;
            var content =  NSObjetJohn.convert(toJsonData: dic);
            map["content"] = content;
            
            return true;
        }else{
            map["msgId"] = msgId;
            return true;
        }
  
    }
    
    /**
        * 获取服务器响应消息ID
        *
        * @param isTgame isTgame 是否是提提电竞IM
        * @param json    服务器响应的json
        * @return
        */
    public static func getRespMsgId(isTgame:Bool, json:String)->String {
         
        if(isTgame){
            
            let dic = NSObjetJohn.dictionary(withJsonString: json);
            if(dic.keys.contains("content")){
                
                let contentMap = NSObjetJohn.dictionary(withJsonString: dic["content"]  as! String);
             
                if(contentMap.keys.contains("msgId")){
                    
                    if(contentMap["msgId"] as! NSObject == NSNull()){
                        return "";
                    }else{
                        return contentMap["msgId"] as! String;
                    }
                    
                  
                   
                }
                
            }
            
        }else{
            let respBean:RespBean = MsgUtils.parseToMsgRespBean(json: json);
            return respBean.msgId ?? "";
        }
        
        return "";
        
       }
    
    
}
